/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.common.controller;

import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.AttachmentBean;
import com.koron.common.domain.MultipartFileParam;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.common.service.api.AttachmentUploadService;
import com.koron.common.utils.Constant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "attachmentUtil",description = "文件上传")
@RestController
@RequestMapping("attachmentUtil")
public class AttachmentController {

    @Autowired
    AttachmentUploadService attachmentUploadService;
    private  final Logger logger = LoggerFactory.getLogger(AttachmentController.class);

    /**
     * 附件分片上传
     */
    @ApiOperation("附件上传(删除+上传)")
   /* @ApiImplicitParams({
            @ApiImplicitParam(name = "deleteId", value = "要删除的附件ID(多个用逗号拼接)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessId", value = "业务的ID", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "files", value = "附件value", paramType = "query")
    })*/
    @PostMapping("uploadChunck.htm")
    public MessageBean<?> uploadChunck(HttpServletRequest request, @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, MultipartFileParam param) {
        MessageBean msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Object.class);
        try {
            if (StringUtils.isBlank(param.getBusinessId())) {
                return MessageBean.create(30000, "关联的业务id不能未空", Object.class);
            }
            param.setSessionId(request.getSession().getId());
            attachmentUploadService.uploadFileByMappedByteBuffer(user, param);
            //msg.setData(uploadNum);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            msg.setCode(-1);
            msg.setDescription("操作发生异常");
            return msg;
        }
        return msg;
    }


    /**
     * 附件上传
     */
    @ApiOperation("附件上传(删除+上传)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deleteId", value = "要删除的附件ID(多个用逗号拼接)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessId", value = "业务的ID", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "files", value = "附件value", paramType = "query")
    })
    @PostMapping("chuncksResult.htm")
    public MessageBean<?> chuncksResult(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                       @RequestParam(required = false) final String deleteId,
                                       @RequestParam(required = true) final String businessId,
                                       @RequestParam(value="files", required = false) MultipartFile[] files) {
        MessageBean<Object> msg=MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Object.class);
        try {
            Map uploadNum = attachmentUploadService.uploadAttach(user,deleteId,businessId, files);
            msg.setData(uploadNum);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            msg.setCode(-1);
            msg.setDescription("操作发生异常");
            return msg;
        }
        return msg;
    }


    /**
     * 附件上传
     */
    @ApiOperation("附件上传(删除+上传)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deleteId", value = "要删除的附件ID(多个用逗号拼接)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "businessId", value = "业务的ID", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "files", value = "附件value", paramType = "query")
    })
    @PostMapping("uploadAttach.htm")
    public MessageBean<?> uploadAttach(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                       @RequestParam(required = false) final String deleteId,
                                       @RequestParam(required = true) final String businessId,
                                       @RequestParam(value="files", required = false) MultipartFile[] files) {
        MessageBean msg=MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Object.class);
        try {
            Map uploadNum = attachmentUploadService.uploadAttach(user,deleteId,businessId, files);
            msg.setData(uploadNum);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            msg.setCode(-1);
            msg.setDescription("操作发生异常");
            return msg;
        }
        return msg;
    }

    @ApiOperation("附件上传(只上传文件)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessId", value = "业务的ID", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "files", value = "附件value", paramType = "query"),
            @ApiImplicitParam(name = "attModel", value = "附件所属模块", paramType = "query")
    })
    @PostMapping("uploadFiles.htm")
    public MessageBean<?> uploadFiles(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                       @RequestParam(required = true) final String businessId,
                                       @RequestParam(required = false) String attModel,
                                       @RequestParam(value="files", required = false) MultipartFile[] files) {
        MessageBean msg=MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Object.class);
        try {
            msg.setData(attachmentUploadService.uploadFilesByModel(user,businessId, files,attModel));
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            msg.setCode(-1);
            msg.setDescription("操作发生异常");
            return msg;
        }
        return msg;
    }

    @ApiOperation("附件删除(删除业务ID下的附件)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessId", value = "业务ID", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "deleteId", value = "要删除的附件的ID(多个用逗号拼接)", paramType = "query", dataType = "String")
            })
    @PostMapping("deleteFiles.htm")
    public MessageBean<?> deleteFiles(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                      @RequestParam(required = true) final String businessId,
                                      @RequestParam(required = true) final String deleteId) {
        MessageBean msg=MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Object.class);
        try {
            msg.setData(attachmentUploadService.deleteFiles(user,businessId, deleteId));
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            msg.setCode(-1);
            msg.setDescription("操作发生异常");
            return msg;
        }
        return msg;
    }

    /**
     * 附件列表查询
     */
    @ApiOperation("附件列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessId", value = "业务ID", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "attModel", value = "模块id", paramType = "query", dataType = "String")
    })
    @GetMapping({"getAttachList.htm","smMb/getAttachList.htm"})
    public MessageBean<?> getAttachList(@RequestParam(required = true) final String businessId,
                                        @RequestParam(required = false) String attModel) {
        MessageBean msg=MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Object.class);
        try {
            List<AttachmentBean> attachList=attachmentUploadService.getAttachListByModel(businessId,attModel);
            msg.setData(attachList);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            msg.setCode(-1);
            msg.setDescription("发生异常");
            return msg;
        }
        return msg;
    }


}
